package com.xlong.k329;

import android.os.Bundle;
import android.os.Message;
import android.util.Log;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

//连接mySql数据库方法
public class MysqlDBUtils {
    private static final String TAG ="MysqlDBUtils";
    private static String driver="com.mysql.jdbc.Driver";
    //这里是MySQL的用户名
    private static String user="root";
    private static String password = "prevail";

    public static Connection getConn(String dbName){
        Connection connection = null;
        try{
            Class.forName(driver);
            //数据的IP地址，本文中的地址不是我的真实地址，请换为你的真实IP地址。
            String ip="127.0.0.1";
            String port="3306";
            String url = "jdbc:mysql://" + ip + ":" + port +"/" + dbName;
            connection= DriverManager.getConnection(url, user, password);
            Log.e("数据库连接", "成功!");
        } catch (Exception e) {
            Log.e("数据库连接", "失败!");
            e.printStackTrace();
        }
        return connection;
    }
    public static String[] getEthData() {
        String rsdata[]=new String[2];
        Connection connection = getConn("protables");
        if (connection!=null){
            String sql="select username,realname from users";
            try{
                java.sql.Statement statement = connection.createStatement();
                ResultSet rSet = statement.executeQuery(sql);
                while (rSet.next()) {
                    rsdata[0]=rSet.getString("username");
                    rsdata[1]=rSet.getString("realname");
                    Log.e(TAG,"数组组装成功");
                }
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        };
        Log.e(TAG,"数组已返回");
        Message msg = Message.obtain();
        Bundle bundle = new Bundle();
        msg.what = 0x123;
        bundle.putString("username", rsdata[0]);
        bundle.putString("realname", rsdata[1]);

        return rsdata;
    }
    public static String getVersion() {
        String version=null;
        Connection connection = getConn("eth");
        if (connection!=null){
            String sql="select version from version";
            try{
                java.sql.Statement statement = connection.createStatement();
                ResultSet rSet = statement.executeQuery(sql);
                while (rSet.next()) {
                    version=rSet.getString("version");//日期
                    Log.e(TAG,"数组组装成功==="+version);
                }
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        };
        Log.e(TAG,"数组已返回==========");
        return version;
    }

}
